package com.bigdata;

public class ThreadSafeDemo extends Thread {

    private int num = 0;


    public void incrNum() {
        for (int i = 0; i < 500; i++) {
            num++;
        }
    }

    @Override
    public void run() {
        incrNum();
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadSafeDemo t1 = new ThreadSafeDemo();
        ThreadSafeDemo t2 = new ThreadSafeDemo();

        // t1和t2是2个线程，自增也是自增自己内部的nun，隔离开的
        t1.start();
        t2.start();

        Thread.sleep(500);

        System.out.println(t1.num);
        System.out.println(t2.num);

    }


    public static void main1(String[] args) {
        ThreadSafeDemo demo = new ThreadSafeDemo();
        demo.incrNum();
        demo.incrNum();
        demo.incrNum();
        System.out.println(demo.num);
    }

}
